package ch.dreamweb.readpractice.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import ch.dreamweb.readpractice.client.managed.request.ApplicationRequestFactory;
import ch.dreamweb.readpractice.client.managed.request.SettingProxy;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;

public class InfoHolder implements Serializable {

	private final ApplicationRequestFactory requests = GWT
			.create(ApplicationRequestFactory.class);

	/** Comment for <code>serialVersionUID</code>. */
	private static final long serialVersionUID = 8404020609476305195L;

	private ArrayList<String> morphems = new ArrayList<String>();

	private SettingProxy setting;

	private Date startTime;

	private Long settingId;

	private int activeunit = 0;

	private int activetest = 0;

	private int testround = 0;

	private Boolean logoutClicked = false;

	public int[] indexes1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	public int[] indexes2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	public int[] indexes3 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	public int[] indexes4 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	public int unitToEdit = 0;
	public int highestindex1 = 1;
	public int highestindex2 = 1;
	public int highestindex3 = 1;
	public int highestindex4 = 1;
	public Boolean statisticread = false;

	// Private constructor prevents instantiation from other classes
	private InfoHolder() {

	}

	/**
	 * SingletonHolder is loaded on the first execution of
	 * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
	 * not before.
	 */
	private static class Info {
		public static final InfoHolder INSTANCE = new InfoHolder();
	}

	public static InfoHolder getInstance() {
		return Info.INSTANCE;
	}

	public void setMorphems(ArrayList<String> morphems) {
		this.morphems = morphems;
	}

	public ArrayList<String> getMorphems() {
		// Morpheme shuffeln
		String temp1 = "";
		String temp2 = "";
		int random = 999;
		for (int i = 0; i < morphems.size(); i++) {
			do {
				random = (int) (Math.random() * morphems.size());
			} while (i == random);
			temp1 = morphems.get(i);
			temp2 = morphems.get(random);
			morphems.remove(i);
			morphems.add(i, temp2);
			morphems.remove(random);
			morphems.add(random, temp1);
		}
		return morphems;
	}

	public void setSetting(SettingProxy setting) {
		this.setting = setting;
	}

	public SettingProxy getSetting() {
		return setting;
	}

	public void setStartTime() {
		startTime = new Date();
		Log.info("New unit started at: " + startTime);
	}

	public boolean isTimeUp(int tottime) {
		Long timeDiff = tottime * 1000l;
		Long timePassed = new Date().getTime() - startTime.getTime();
		return timePassed > timeDiff;
	}

	public Long getSettingId() {
		return settingId;
	}

	public void setSettingId(Long settingId) {
		this.settingId = settingId;
	}

	public void setUnitActive(int unit) {
		activeunit = unit;
	}

	public int getActiveUnit() {
		return activeunit;
	}

	public void setTestActive(int test) {
		activetest = test;
	}

	public int getActiveTest() {
		return activetest;
	}

	public void incActiveTest() {
		activetest++;
	}

	public void setOnLogoutButtonClicked(Boolean clicked) {
		logoutClicked = clicked;
	}

	public Boolean getOnLogoutButtonClicked() {
		return logoutClicked;
	}

	public int getTestround() {
		return testround;
	}

	public void setTestround(int testroundfromsetting) {
		testround = testroundfromsetting;
	}
}
